﻿using SqlSugar;
using SXICE.Core;
using SXICE.Core.Domains;
using SXICE.Core.Helpers;
using SXICE.Core.Models;
using SXICE.Core.Models.Search;
using SXICE.IRepository;
using System.Collections.Generic;

namespace SXICE.Repository
{
    /// <summary>
    /// 权限菜单仓储
    /// </summary>
    public class MenuRepostiory : DBContext, IMenuRepository
    {
        public ReturnResult<bool> Delete(int id)
        {
            return MenuDB.DeleteById(id).ToResult();
        }

        public ReturnResult<bool> InsertOne(Menu obj)
        {
            return MenuDB.Insert(obj).ToResult();
        }

        public ReturnPageListResult<Menu> Search(PageListQuery<RecordSearchModel> pageListQuery)
        {
            var condition = pageListQuery.Condition ?? new RecordSearchModel();
            var total = 0;
            return Db.Queryable<Menu>().WhereIF(!condition.Name.IsNullOrEmpty(), t => t.Name.Contains(pageListQuery.Condition.Name))
                    .WhereIF(!condition.Code.IsNullOrEmpty(), m => m.Code.Contains(condition.Code))
                    .OrderBy(t => t.Id, OrderByType.Desc).ToPageList(pageListQuery.PageIndex, pageListQuery.PageSize, ref total).ToResult(total);
        }

        public ReturnResult<List<Menu>> Search()
        {
            return Db.Queryable<Menu>().OrderBy(t => t.Id, OrderByType.Desc).ToList().ToResult();
        }
    }
}